package com.ly.drivingschool.service.impl;

import com.ly.drivingschool.dao.OrderDao;
import com.ly.drivingschool.pojo.Order;
import com.ly.drivingschool.pojo.OrderPage;
import com.ly.drivingschool.pojo.Pager;
import com.ly.drivingschool.pojo.User;
import com.ly.drivingschool.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    /**
     * 分页查询 驾校订单信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public Pager<OrderPage> findByPager(int page, int size) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", (page-1)*size);
        params.put("size", size);
        Pager<OrderPage> pager = new Pager<OrderPage>();
        List<OrderPage> list = orderDao.findByPager(params);
        pager.setData(list);
        pager.setCount(orderDao.count());
        return pager;
    }

    /**
     * 模糊查询
     * @param page
     * @param size
     * @param keyWork
     * @return
     */
    @Override
    public Pager<OrderPage> findByKey(int page, int size, String keyWork) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", (page-1)*size);
        params.put("size", size);
        params.put("keyWork", keyWork);
        Pager<OrderPage> pager = new Pager<OrderPage>();
        List<OrderPage> list = orderDao.findByKey(params);
        pager.setData(list);
        pager.setCount(orderDao.countKey(keyWork));
        return pager;
    }

    /**
     * 选择条件查询 dd001 订单状态
     * @param page
     * @param size
     * @param col
     * @param code
     * @return
     */
    @Override
    public Pager<OrderPage> findByCondition(int page, int size, String col, String code) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", (page-1)*size);
        params.put("size", size);
        params.put("col", col);
        params.put("code", code);
        Pager<OrderPage> pager = new Pager<OrderPage>();
        List<OrderPage> list = orderDao.findByCondition(params);
        pager.setData(list);
        pager.setCount(orderDao.countDD001(col,code));
        return pager;
    }

    /**
     * 查询一条记录
     * @param userId
     * @return
     */
    @Override
    public OrderPage selectByPrimaryKey(Integer userId) {
        OrderPage order = orderDao.selectByPrimaryKey(userId);
        return order;
    }

    /**
     * 根据id更新一条记录
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(Order record) {
        int code = orderDao.updateByPrimaryKeySelective(record);
        if (code == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    /**
     * 添加新的订单
     * @param record
     * @return
     */
    @Override
    public int insertSelective(Order record) {
        int code = orderDao.insertSelective(record);
        System.out.println("返回的值：" + code);
        if (code == 0) {
            return 0;
        } else {
            // 成功
            return 1;
        }
    }
}
